package dsync;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.InvalidPropertiesFormatException;

import dsync.SFile.*;

public class dsync {
	
	private SDir dir;
	
	public dsync()
	{
		
		try
		{
			dir = new SDir();
		}
		catch(InvalidPropertiesFormatException e)
		{
			System.out.print("Invalid Format config file\n");
			System.exit(-1);
		}
		catch(FileNotFoundException e)
		{
			System.out.print("File not found\n");
			System.exit(-1);
		}
		catch(IOException e)
		{
			System.out.print("IO error\n");
			System.exit(-1);
		}
	}
	
	public dsync(String src, String dst)
	{
		try
		{
			dir = new SDir(src, dst);
		}
		catch(InvalidPropertiesFormatException e)
		{
			System.out.print("Invalid Format config file\n");
			System.exit(-1);
		}
		catch(FileNotFoundException e)
		{
			System.out.print("File not found\n");
			System.exit(-1);
		}
		catch(IOException e)
		{
			System.out.print("IO error\n");
			System.exit(-1);
		}
	}
	
	public void Update_no_copy()
	{
		try
		{
			dir.updateAll(false);
		}
		catch(IOException e)
		{
			System.out.print("Update error\n");
			System.exit(-1);
		}
		
	}
	
	public void Update_copy()
	{
		try
		{
			dir.updateAll(true);
		}
		catch(IOException e)
		{
			System.out.print("Update error\n");
			System.exit(-1);
		}
		
	}
	
	
	public static void Usage()
	{
		//Usage
		System.out.print("Usage:\n\n");
		System.out.print("-help - out terminal this message\n");
		System.out.print("-cp - use copy synchronization\n");
		System.out.print("\n");
		System.out.print("./dsync source_directory destination_directory\n");
		System.out.print("./dsync source_directory destination_directory -cp\n");
		System.exit(0);
	}
	
	public static void main(String[] args){
		
		dsync sync;
		
		//default sync
		
		if(args.length == 0)
		{
			sync = new dsync();
			sync.Update_no_copy();
		}
		else
		{
			if(args.length == 1)
			{
				if(args[0].equals("-help"))
				{
					Usage();
				}
				
				if(args[0].equals("-cp"))
				{
					sync = new dsync();
					sync.Update_copy();
				}
			}
			
			if(args.length == 2)
			{
				sync = new dsync(args[0], args[1]);
				sync.Update_no_copy();
			}
			if(args.length == 3)
			{
				int i = 0;
				for(i=0; i< 3; i++)
				{
					if(args[i].equals("-cp")){
						if(i == 0){
							
							sync = new dsync(args[1], args[2]);
							sync.Update_copy();
						}
						if( i == 2){
							sync = new dsync(args[0], args[1]);
							sync.Update_copy();
						}
						if( i == 1){
							sync = new dsync(args[0], args[2]);
							sync.Update_copy();
						}
					} 
				}
			}
		}

	}

}
